﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HAUtilWpf;

namespace HA.ModFlow.Controls.UserControls
{
    /// <summary>
    /// ucArray.xaml 的交互逻辑
    /// </summary>
    public partial class ucArray : UserControl
    {
        public ucArray()
        {
            InitializeComponent();


            #region 初始化datagrid的一些属性

            this.dgData.CanUserAddRows = false;
            //this.dgData.CanUserPasteToNewRows = false;
            this.dgData.GridLinesVisibility = DataGridGridLinesVisibility.None;

            this.dgData.SelectionUnit = DataGridSelectionUnit.Cell;

            //右键菜单

            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "复制";
            itemCopy.Command = ApplicationCommands.Copy;

            //MenuItem itemPaste = new MenuItem();
            //itemPaste.Header = "粘贴";
            //itemPaste.Command = ApplicationCommands.Paste;

            MenuItem itemPaste2 = new MenuItem();
            itemPaste2.Header = "粘贴";
            itemPaste2.Click += new RoutedEventHandler(itemPaste2_Click);

            ContextMenu cm = new ContextMenu();
            cm.Items.Add(itemCopy);
            //cm.Items.Add(itemPaste);

            cm.Items.Add(itemPaste2);
            this.dgData.ContextMenu = cm;


            this.dgData.LoadingRow += new EventHandler<DataGridRowEventArgs>(dgData_LoadingRow);

            this.dgData.AutoGeneratingColumn += new EventHandler<DataGridAutoGeneratingColumnEventArgs>(dgData_AutoGeneratingColumn);

            this.dgData.AutoGeneratedColumns += new EventHandler(dgData_AutoGeneratedColumns);

            #endregion
        }

        void dgData_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            DataGridTextColumn column = e.Column as DataGridTextColumn;
            Binding binding = column.Binding as Binding;
            binding.Path = new PropertyPath(binding.Path.Path + ".Value");
            //if (this._type == DataType.DOUBLE)
            //{
            //    binding.Converter = new NumberToStringConverter();
            //}
            
        }

        void dgData_AutoGeneratedColumns(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dgData.Columns.Count; i++)
            {
                this.dgData.Columns[i].Header = (i + 1).ToString();
                //dgData.SetResourceReference(DataGrid.ColumnHeaderStyleProperty, "DataGridHeaderStyle");
                //dgData.SetResourceReference(DataGrid.CellStyleProperty, "DataGridCellStyle");
            }
        }


        //配置datagrid


        void dgData_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }









        private void itemPaste2_Click(object sender, RoutedEventArgs e)
        {
            //取得剪贴板数据


            List<string[]> rowData = ClipboardHelper.ParseClipboardData();

            //取得选中单元的行序号

            int minRowIndex = DataGridHelper.GetRowIndex(this.dgData.SelectedCells[0]);

           //下面的代码写的太过蹊跷，应该取得的是选中的行号吧?

            
            int maxRowIndex = this.dgData.Items.Count - 1;

            int minColumnDisplayIndex = this.dgData.SelectedCells[0].Column.DisplayIndex;

            int maxColumnDisplayIndex = this.dgData.Columns.Count - 1;



            int rowDataIndex = 0;
            if (this._type == DataType.INT)
            {
                for (int i = minRowIndex; i <= maxRowIndex && rowDataIndex < rowData.Count; i++, rowDataIndex++)
                {
                    int columnDataIndex = 0;
                    for (int j = minColumnDisplayIndex; j <= maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++)
                    {
                        if (rowData[rowDataIndex][columnDataIndex] != "")
                        {


                            int value = 0;

                            bool b = int.TryParse(rowData[rowDataIndex][columnDataIndex], out value);

                            if (b)
                            {

                                this._intData[i, j] = value;



                            }

                            else
                            {
                                continue;

                            }


                           
                        }
                    }
                }
                this.dgData.Items.Refresh();
            }
            else
            {
                for (int i = minRowIndex; i <= maxRowIndex && rowDataIndex < rowData.Count; i++, rowDataIndex++)
                {
                    int columnDataIndex = 0;
                    for (int j = minColumnDisplayIndex; j <= maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++)
                    {
                        if (rowData[rowDataIndex][columnDataIndex] != "")
                        {


                            double value = 0;

                            bool b = double.TryParse(rowData[rowDataIndex][columnDataIndex], out value);

                            if (b)
                            {

                                this._doubleData[i, j] = value;



                            }

                            else
                            {
                                continue;

                            }

                           
                           
                        }
                    }
                }
                this.dgData.Items.Refresh();
            }
        }







        //正常的数据

        PCGridArray<double> _arrays;

        public PCGridArray<double> Arrays
        {
            get { return _arrays; }
            set
            {
                _arrays = value;

                this.OnSetGridArray();

            }
        }


        protected void OnSetGridArray()
        {

            this.dgData.Columns.Clear();

        }



        double[,] _doubleData;




        int[,] _intData;


        DataType _type;

        public DataType Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public void SetIntData(int[,] datas)
        {

            this._type = DataType.INT;

            this._intData = datas;

        }

        public void SetDoubleData(double[,] datas)
        {

            this._type = DataType.DOUBLE;

            this._doubleData = datas;

        }

    }
}
